"""
根据遗传相似度矩阵，绘制单样本与其他样本遗传相似度的分布直方图
"""

import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data_path=sys.argv[1]
sample_name=sys.argv[2]

output = "/".join(data_path.split("/")[:-1])

df = pd.read_csv(data_path)

def image_plot(sample_name):
    data = df[sample_name].tolist()
    plt.figure(figsize=(8, 6), dpi=300)  # 设置图表大小
    counts, bins, patches = plt.hist(data, bins=30, alpha=1, color='#217CFF', edgecolor='#C6C6C6')  # 绘制直方图，bins为直方图的柱数

    # 找到最高的频次及其对应的区间
    max_count = np.max(counts)
    max_bin = bins[np.where(counts == max_count)[0][0]]

    # 在直方图上标注最高的频次
    plt.annotate(round(max_bin, 3), xy=(max_bin, max_count), xytext=(-10, 0), textcoords='offset points')

    # 添加图表标题和轴标签
    plt.title(sample_name)
    plt.xlabel('Similarity')
    plt.ylabel('Frequency')
    plt.savefig(f"{output}/{sample_name}.png", dpi=300, format="png")
    plt.close()


image_plot(sample_name)
